package de.infotheo;

import java.math.BigInteger;
import java.util.Vector;

/**
 * The class OrderOfGroup contains a method to calculate the order of a group
 * @author Eugen
 *
 */
public class OrderOfGroup {
	
	private final static BigInteger one = new BigInteger("1");
	
	/**
	 * The method calcOrderOfGroup calculates and returns the order of a group as a
	 * BigInteger parameter
	 * 
	 * @param n
	 *            The number to calculate the order of
	 * @return order of n
	 */
	public BigInteger calcOrderOfGroup(BigInteger n) {
		// create an object to store the set of the group elements
		Vector<BigInteger> arrayVec = new Vector<BigInteger>();
		// iterate from zero to n and add all elements to the set, which have the gcd of one with n 
		for (BigInteger a = new BigInteger("1"); a.compareTo(n) <=0; a=a.add(one)) {

			//compute the greatest common divisor
			if (n.gcd(a).compareTo(one) == 0) {
				arrayVec.add(a);
			}
		}

		System.out.println("The order of Z_" + n + " is: " + arrayVec.size());
		
		return new BigInteger(Integer.toString(arrayVec.size()));

	}

}
